/*
 * SpaceTiger.java
 *
 * Created on 23. Oktober 2006, 19:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tor.screens;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.AnimatedSprite;
import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;
import com.golden.gamedev.object.background.ParallaxBackground;
import com.golden.gamedev.object.collision.BasicCollisionGroup;
import com.golden.gamedev.object.sprite.VolatileSprite;
import de.tor.loaders.Level;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 *
 * @author Charon
 */
public class SpaceTigerGame extends GameObject{
    
    PlayField        playfield;         // the game playfield
    ParallaxBackground       background;
    
    SpriteGroup      PLAYER_GROUP;
    SpriteGroup      PROJECTILE_GROUP;
    SpriteGroup      ENEMY_GROUP;
    
    AnimatedSprite     plane;
    
    Timer            moveTimer;        // to set enemy behaviour
    // for moving left to right, right to left
    
    ProjectileEnemyCollision2 collision;
    
    
    GameFont           font;
    
    public SpaceTigerGame(GameEngine parent){
        super(parent);
    }
    /****************************************************************************/
    /**************************** GAME SKELETON *********************************/
    /****************************************************************************/

    private Level level;
    
    public void initResources() {
        // create the game playfield
      
        // associate the playfield with a background
        level = new Level(parent);
        level.initResources();        
      
      //  PROJECTILE_GROUP = new SpriteGroup("Projectile");

       // playfield.addGroup(PROJECTILE_GROUP);
       // use shortcut, creating group and adding it to playfield in one step
 //       ENEMY_GROUP = playfield.addGroup(new SpriteGroup("Enemy"));
        
        // inserts sprites in rows to ENEMY_GROUP
    //    BufferedImage image = getImage("resources/tiger.gif");
        
    /*    int startX = 10, startY = 9*960;     // starting coordinate
        for (int j=0;j < 1;j++) {         // 4 rows
            for (int i=0;i < 1;i++) {     // 7 sprites in a row
                Sprite enemy = new Sprite(image, startX+(i*30), startY+(j*30));
                enemy.setHorizontalSpeed(0.04);
                enemy.setVerticalSpeed(0.04);
                ENEMY_GROUP.add(enemy);
            }
        }*/
        
    
        
        // init the timer to control enemy sprite behaviour
        // (moving left-to-right, right-to-left)
        moveTimer = new Timer(2000); // every 2 secs the enemies reverse its speed
        
        
 /*       /////// register collision ///////
        collision = new ProjectileEnemyCollision2(this);
        // register collision to playfield
        playfield.addCollisionGroup(PROJECTILE_GROUP, ENEMY_GROUP, collision);
     */   
        
        font = parent.fontManager.getFont(getImages("resources/font.png", 20, 3),
                " !            .,0123" +
                "456789:   -? ABCDEFG" +
                "HIJKLMNOPQRSTUVWXYZ ");
    }
    
    double shootTime = 60;
    
    public void update(long elapsedTime) {
        level.update(elapsedTime);
        // enemy sprite movement timer
  /*      if (moveTimer.action(elapsedTime)) {
            // reverse all enemies' speed
            Sprite[] sprites = ENEMY_GROUP.getSprites();
            int size = ENEMY_GROUP.getSize();
            
            // iterate the sprites
            for (int i=0;i < size;i++) {
                // reverse sprite velocity
                sprites[i].setHorizontalSpeed(-sprites[i].getHorizontalSpeed());
            }
        }*/
        
        if (keyDown(KeyEvent.VK_ESCAPE)) {
            System.exit(0);
        }
        
        
        
        // firing!!
      
        
       /* // toggle ppc
        if (keyPressed(KeyEvent.VK_ENTER)) {
            collision.pixelPerfectCollision = !collision.pixelPerfectCollision;
        }*/
    }
    
    
    
    public void render(Graphics2D g) {
      //  playfield.render(g);
        level.render(g);
        
        // draw info text
        font.drawString(g, "ARROW KEY : MOVE", 10, 10);
        font.drawString(g, "CONTROL   : FIRE", 10, 30);
        font.drawString(g, "ENTER     : TOGGLE PPC", 10, 50);
        
       /* if (collision.pixelPerfectCollision) {
            font.drawString(g, " USE PIXEL PERFECT COLLISION ", GameFont.RIGHT, 0, 460, getWidth());
        }*/
    }
    
    
    /****************************************************************************/
    /***************************** START-POINT **********************************/
    /****************************************************************************/
    
  /*  public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new SpaceTiger(), new Dimension(640,480), false);
        game.start();
    }*/
    
}


class ProjectileEnemyCollision2 extends BasicCollisionGroup {
    
    
    SpaceTigerGame    owner;
    
    public ProjectileEnemyCollision2(SpaceTigerGame owner) {
        this.owner = owner; // set the game owner
        // we use this for getting image and
        // adding explosion to playfield
    }
    
    // when projectiles (in group a) collided with enemy (in group b)
    // what to do?
    public void collided(Sprite s1, Sprite s2) {
        // we kill/remove both sprite!
        /*s1.setActive(false); // the projectile is set to non-active
        s2.setActive(false); // the enemy is set to non-active
         
        // show explosion on the center of the exploded enemy
        // we use VolatileSprite -> sprite that animates once and vanishes afterward
        BufferedImage[] images = owner.getImages("resources/explosion.png", 7, 1);
        VolatileSprite explosion = new VolatileSprite(images, s2.getX(), s2.getY());
         
        // directly add to playfield without using SpriteGroup
        // the sprite is added into a reserved extra sprite group in playfield
        // extra sprite group is used especially for animation effects in game
        owner.playfield.add(explosion);*/
        s2.setActive(false);
        BufferedImage[] images = owner.getImages("resources/tiger_explosion.gif", 5, 1);
        VolatileSprite explosion = new VolatileSprite(images, s2.getX(), s2.getY());
        owner.playfield.add(explosion);
        
        s1.setHorizontalSpeed(-s1.getHorizontalSpeed());
    }
    
}